<?php

/**

 * Some people get confused, and think factories/modules ARE stand-alone

 * plugins. If they try to activate this factory as if it was a plugin, they will

 * be shown a message explaining how to install aLinks.

 */

if (!defined('ALINKS_RUNNING')) {

	/**

	Plugin Name: Default Factory

	Description: aLinks factory component

	*/

	add_action('admin_notices', create_function(null, '

	?>

	<div id="alinks_notice" class="updated fade-ff0000">

		<p><strong>Error</strong><br />

		You activated an aLinks factory as a plugin. It is a component of the aLinks. It is not a WordPress plugin. You must have aLinks installed to

		use this.</p><p>You can download aLinks from <a href="http://alinks.headzoo.com/download">here</a>. Once you have aLinks installed

		and activated, you can install this factory following the directions in the <a href="http://alinks.headzoo.com/documentation">documentation</a>.</p?

	</div>

	<?php

	'));

	return;

}



/**

 * @see Alinks_Factory_Abstract

 */

require_once('Alinks/Factory/Abstract.php');



/**

 * Default Factory

 *

 * Factory class for defining keyphrases that are replaced with

 * a defined URL.

 *

 * This class can also be studied to learn how to create factories.

 *

 * @author Sean Hickey <sean@headzoo.com>

 * @version $Id: factory.php 396 2008-07-04 21:44:45Z sean $

 */

class Alinks_Factory_Default extends Alinks_Factory_Abstract

{

	/**

	 * The name of the factory

	 *

	 * @var string

	 */

	protected $_name = 'Default';

	

	/**

	 * The factory safe name

	 *

	 * @var string

	 */

	protected $_safe_name = 'default';

	

	/**

	 * The URL for the factory

	 *

	 * @var string

	 */

	protected $_url = 'http://alinks.headzoo.com/factories/default';

	

	/**

	 * A short description of the factory

	 *

	 * @var string

	 */

	protected $_description = 'A simple factory that replaces keyphrases with a defined URL.';

	

	/**

	 * The factory authors

	 *

	 * @var array

	 */

	protected $_authors = array('name' => 'Sean Hickey', 'url' => 'http://headzoo.com');

	

	/**

	 * The factory version

	 *

	 * @var float

	 */

	protected $_version = 2.0;

	

	/**

	 * The version of aLinks the factory is compatible with

	 *

	 * @var string

	 */

	protected $_alinks_version = '2.*';

	

	/**

	 * Whether this factory has a settings form

	 *

	 * @var bool

	 */

	protected $_has_settings_form = true;

	

	/**

	 * Whether this factory has a keyphrases form

	 *

	 * @var bool

	 */

	protected $_has_keyphrases_form = true;

	

	/**

	 * The blog host name

	 *

	 * @var string

	 */

	protected $_home_host = null;

	

	/**

	 * Returns the form information for the keyphrases page

	 *

	 * @var Alinks_Form

	 */

	public function getKeyphrasesForm()

	{

		$form = new Alinks_Form($this);

		$phrase = new Alinks_Form_Element_Text('phrase', null, 'Phrase: ');

		$phrase->setRequired(true);

		$form->addElement($phrase);

		

		$description = new Alinks_Form_Element_Text('description', null, 'Description: ');

		$form->addElement($description);

		

		$url = new Alinks_Form_Element_Text('url', null, 'URL: ');

		$url->setDescription('The URL the clicked link will go to.');

		$url->setRequired(true);

		$form->addElement($url);

		

		return $form;

	}

	

	/**

	 * Returns the settings form for this factory

	 *

	 * @return Alinks_Form

	 */

	public function getSettingsForm()

	{

		$form = new Alinks_Form($this);

		$external = new Alinks_Form_Element_Checkbox('external', array(), 'Show External Link Image');

		$external->setDescription('Add a small icon next to links that go to external sites.');

		$form->addElement($external);

		

		$group = new Alinks_Form_Element_RadioGroup('external_image_color', 'External Link Color');

		$group->setDescription('The image color to use for the external link image, if you turn that option on.');

		$form->addElement($group);

		

		$radio = new Alinks_Form_Element_Radio(null, null, 'Blue');

		$radio->setValue('blue');

		$group->addRadio($radio);

		

		$radio = new Alinks_Form_Element_Radio(null, null, 'Red');

		$radio->setValue('red');

		$group->addRadio($radio);

		

		$radio = new Alinks_Form_Element_Radio(null, null, 'Green');

		$radio->setValue('green');

		$group->addRadio($radio);

		

		return $form;

	}

	

	/**

	 * Replaces a keyphrase with a link

	 *

	 * @param Alinks_Keyphrase_Interface $keyphrase The keyphrase

	 * @param Alinks_LinkBuilder_Interface $builder The link builder

	 * @return Alinks_LinkBuilder_Interface

	 */

	public function parse(Alinks_Keyphrase_Interface $keyphrase, Alinks_LinkBuilder_Interface $builder)

	{

		/**

		 * Check if the link is external. If it is, add the 

		 * alinks_external_link class to the link.

		 */

		if (stristr($keyphrase->getOptions('url'), 'http://') && isset($this->_settings['external']) && $this->_settings['external']) {

			if (null === $this->_home_host) {

				/**

				 * @see Alinks_Settings

				 */

				require_once('Alinks/Settings.php');

				$home_parts = parse_url(Alinks_Settings::get('alinks', 'home'));

				$this->_home_host = $home_parts['host'];

			}

			

			$url_parts = parse_url($keyphrase->getOptions('url'));

			if ($this->_home_host != $url_parts['host']) {

				$url = Alinks_Registry::get('alinks_url') . '/factories/default';

				$color = ($this->_settings['external_image_color']) ? $this->_settings['external_image_color'] : 'blue';

				$image = 'external_' . $color . '.gif';

				$builder->addArg('style', "background: url($url/$image) right center no-repeat;padding-right: 14px;");

			}

		}

		

		return $builder->setUri($keyphrase->getOptions('url'));

	}

}

